<?php

/**
* 套装稿件
*/

define('IN_ECS', true);
define('DF_ACT_ID', 9999);
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
$exc = new exchange($ecs->table("goods_bb"), $db, 'bb_id', 'bb_name');
$type = 2;//活动类型
/*------------------------------------------------------ */
//-- 添加设计风格
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{

    $data     = array('bb_id'=>'0', 'bb_name' => '', 'bb_desc' => '');

    $smarty->assign('data',      $data);
    $smarty->assign('ur_here',      $_LANG['goods_bb2_add']);
    $smarty->assign('action_link',  array('text' => $_LANG['goods_bb2_list'], 'href'=>'goods_bb2.php?act=list'));
    $smarty->assign('form_action',  'insert');

    assign_query_info();
    $smarty->display('goods_bb2_info.htm');
}

elseif ($_REQUEST['act'] =='insert')
{
    /* 权限判断 */
    admin_priv('package_manage');

    $sql = "SELECT COUNT(*) ".
           " FROM " . $ecs->table('goods_bb').
           " WHERE bb_name='" . $_POST['bb_name'] . "'" ;
    if ($db->getOne($sql))
    {
        sys_msg(sprintf($_LANG['data_exist'],  $_POST['bb_name']) , 1);
    }

    /* 插入数据 */
	$record = array('bb_name' => $_POST['bb_name'],'bb_type'=>$type, 'bb_desc' => $_POST['bb_desc'],'add_time'=>gmtime());
    $db->AutoExecute($ecs->table('goods_bb'),$record,'INSERT');

    /* 返回ID */
    $data_id = $db->insert_id();

    admin_log($_POST['bb_name'],'add','goods_bb');
    $link[] = array('text' => $_LANG['back_list'], 'href'=>'goods_bb2.php?act=list');
    $link[] = array('text' => $_LANG['continue_add'], 'href'=>'goods_bb2.php?act=add');
    sys_msg($_LANG['add_succeed'],0,$link);
}

/*------------------------------------------------------ */
//-- 编辑活动
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    //admin_priv('package_manage');
	$id = $_REQUEST['id'];
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('goods_bb') ." WHERE bb_id='$id' ";
    $data = $GLOBALS['db']->getRow($sql);

    $smarty->assign('data',           $data);
    $smarty->assign('ur_here',           $_LANG['data_edit']);
    $smarty->assign('action_link',       array('text' => $_LANG['goods_bb2_list'], 'href'=>'goods_bb2.php?act=list'));
    $smarty->assign('form_action',       'update');

    assign_query_info();
    $smarty->display('goods_bb2_info.htm');

}
elseif ($_REQUEST['act'] =='update')
{
    /* 权限判断 */
    //admin_priv('package_manage');

    /* 检查活动重名 */
    $sql = "SELECT COUNT(*) ".
           " FROM " . $ecs->table('goods_bb').
           " WHERE bb_name='" . $_POST['bb_name'] . "' AND bb_id <> '" .  $_POST['id'] . "'" ;
    if ($db->getOne($sql))
    {
        sys_msg(sprintf($_LANG['data_exist'],  $_POST['bb_name']) , 1);
    }

    /* 更新数据 */
    $record = array('bb_name' => $_POST['bb_name'], 'bb_desc' => $_POST['bb_desc']);
    $db->autoExecute($ecs->table('goods_bb'), $record, 'UPDATE', "bb_id = '" . $_POST['id']."'" );

    admin_log($_POST['bb_name'],'edit','goods_bb');
   $link[] = array('text' => $_LANG['back_list'], 'href'=>'goods_bb2.php?act=list');
    sys_msg($_LANG['edit_succeed'],0,$link);
}

/*------------------------------------------------------ */
//-- 删除指定的活动
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('package_manage');
    $id = intval($_GET['id']);

    $exc->drop($id);

    $url = 'goods_bb2.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 设计风格列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here',      $_LANG['goods_bb2_list']);
    $smarty->assign('action_link',  array('text' => $_LANG['goods_bb2_add'], 'href'=>'goods_bb2.php?act=add'));

    $packages = get_goods_bb_list($type);
    $smarty->assign('data_list',    $packages['data_list']);
    $smarty->assign('filter',       $packages['filter']);
    $smarty->assign('record_count', $packages['record_count']);
    $smarty->assign('page_count',   $packages['page_count']);

    $sort_flag  = sort_flag($packages['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    $smarty->assign('full_page',    1);
    assign_query_info();
    $smarty->display('goods_bb2_list.htm');
}

/*------------------------------------------------------ */
//-- 查询、翻页、排序
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{	
    $packages = get_goods_bb_list($type);
    $smarty->assign('data_list',    $packages['data_list']);
    $smarty->assign('filter',       $packages['filter']);
    $smarty->assign('record_count', $packages['record_count']);
    $smarty->assign('page_count',   $packages['page_count']);

    $sort_flag  = sort_flag($packages['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('goods_bb2_list.htm'), '',
        array('filter' => $packages['filter'], 'page_count' => $packages['page_count']));
}

/*------------------------------------------------------ */
//-- 编辑活动名称
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit_bb_name')
{
    check_authz_json('package_manage');

    $id = intval($_POST['id']);
    $val = json_str_iconv(trim($_POST['val']));

    /* 检查活动重名 */
    $sql = "SELECT COUNT(*) ".
           " FROM " . $ecs->table('goods_bb').
           " WHERE 1=1 "." AND bb_name='$val' AND bb_id <> '$id'" ;
    if ($db->getOne($sql))
    {
        make_json_error(sprintf($_LANG['data_exist'],  $val));
    }

    $exc->edit("bb_name='$val'", $id);
    make_json_result(stripslashes($val));
}



/**
 * 获取表 goods_bb 数据列表
 *
 * @access  $type  = 1 设计风格，2：活动类型
 *
 * @return void
 */
function get_goods_bb_list($type = 1)
{
        $day = getdate();
        $today = local_mktime(23, 59, 59, $day['mon'], $day['mday'], $day['year']);
		$filter =  array();

		$filter['keyword']          = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        $filter['sort_by']          = empty($_REQUEST['sort_by']) ? 'bb_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']       = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        /* 记录总数 */
        $sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('goods_bb'). " WHERE bb_type = ".$type;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        $sql = "SELECT * ".
                    " FROM " . $GLOBALS['ecs']->table('goods_bb') . " WHERE bb_type = '$type' " .
                    " ORDER BY $filter[sort_by] $filter[sort_order] ".
                    " LIMIT " . $filter['start'] . ",$filter[page_size]";

        $filter['keyword'] = stripslashes($filter['keyword']);
        set_filter($filter, $sql, '');

		$row = $GLOBALS['db']->getAll($sql);

		return array('data_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}



?>